#! /bin/bash

# This script make both full and incremental backups for a given directory.
# The output file will have the following format:
#        directory_name.date.full.tarz   	    in case of full backup
#        directory_name.date.incremental.tarz       in case of incremental backup
#-------------------------------------------------------------------------
# Author:   Andres Medina 
#-------------------------------------------------------------------------
# Last updated on : 16-December-2011
# 
#-------------------------------------------------------------------------

# path to binaries
TAR=/bin/tar
MKDIR=/bin/mkdir

#                FUNCTIONS

# makes a backup of the whole directory
#-------------------------------------------------------------------------
full_backup(){  
  # set the backup directory, date and the zip file name
  BACKUP_DIR=$2
  DATE=$(date +%F)
  ZIP_NAME=$2.$DATE.full
  WORKING_DIR=$(pwd)

  # check if the backup directory exist and if it's a directory
  if [ ! -d $WORKING_DIR/$BACKUP_DIR ]; then
    echo "ERROR: The directory '$BACKUP_DIR' doesn't exist or it's not a directory"
    exit 1;
  fi
  
  # create the compress file <archive.tarz> with the format directory_name.date.(f|i).tarz
  $TAR -czf $ZIP_NAME.tarz $BACKUP_DIR
  echo "Full backup file '$ZIP_NAME.tarz' create successfully";

}

# makes a incremental backup of the directory
#------------------------------------------------------------------------- 
partial_backup(){  
  # set the backup directory, date and the zip file name
  BACKUP_DIR=$2
  DATE=$(date +%F)
  ZIP_NAME=$2.$DATE.incremental;
  WORKING_DIR=$(pwd)

  # check if the backup directory exist and if it's a directory
  if [ ! -d $WORKING_DIR/$BACKUP_DIR ]; then
    echo "ERROR: The directory '$BACKUP_DIR' doesn't exist or it's not a directory"
    exit 1;
  fi

  # create the compress file <archive.tarz> with the format directory_name.date.(f|i).tarz, for files  with modification date less than 1 week ago
  $TAR -czf $ZIP_NAME.tarz -N "$(date -d '7 day ago')" $BACKUP_DIR
  echo "Incremental backup file '$ZIP_NAME.tarz' create successfully"

}


# recover the files in the backup file <archive.tarz>
#------------------------------------------------------------------------- 
recover_backup(){
  # set the working directory and the backup file
  BACKUP_FILE=$1
  RECOVER_FILE=$2
  
  # check if the recover file exist
  if [ ! -e ./$BACKUP_FILE ]; then
    echo "ERROR: The file '$BACKUP_FILE' doesn't exist"
    exit 1;
  fi

  # extract the content of the <archive.tarz> file in the working directory 
  FILES_NUM=$($TAR -ztf $BACKUP_FILE | grep /$RECOVER_FILE | wc -l)
  if  [ $FILES_NUM == 0 ]; then
    echo "File not found in the backup archive"
    exit 1;
  fi
  
  if [ $FILES_NUM == 1 ]; then
    for FILES in $($TAR -ztf $BACKUP_FILE | grep /$RECOVER_FILE); do
      OLD=$(pwd)
      $MKDIR aux
      cd aux
      $TAR -xzf $OLD/$BACKUP_FILE $FILES
      mv $FILES $OLD/$RECOVER_FILE
      cd $OLD
      rm -r aux
      echo "File '$RECOVER_FILE' successfully extracted from backup file '$BACKUP_FILE'";  
    done
  fi
  
  if [ $FILES_NUM > 1 ]; then
    echo "$FILES_NUM versions of the file '$RECOVER_FILE' were found in the backup file '$BACKUP_FILE'."
    echo "the files found are the following :"
    echo "$($TAR -ztf $BACKUP_FILE | grep /$RECOVER_FILE | nl)"   
    echo "Please type the number of the version of the file '$RECOVER_FILE' that you want to extract"
    read NUMBER
    NUM=0
    for FILES in $($TAR -ztf $BACKUP_FILE | grep /$RECOVER_FILE); do
      NUM=$(($NUM+1))
      if [ $NUM == $NUMBER ]; then
        OLD=$(pwd)
        $MKDIR aux
        cd aux
        $TAR -xzf $OLD/$BACKUP_FILE $FILES
        mv $FILES $OLD/$RECOVER_FILE
        cd $OLD
        rm -r aux
        echo "File '$RECOVER_FILE' successfully extracted from backup file '$BACKUP_FILE'";
      fi        
    done
  fi

}

#              MAIN PROGRAM
#
#-----------------------------------------------------------------------

# calls the right function according to the flag given by the user
case $1 in 
  f) full_backup $1 $2;;
  i) partial_backup $1 $2;;
  r) recover_backup $2 $3;;
  *) echo "ERROR: wrong syntaxis";;
esac
